package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.Code;
import com.itheima.common.MessageConstant;
import com.itheima.common.PageResult;
import com.itheima.common.Result;
import com.itheima.dto.DishDTO;
import com.itheima.entity.Dish;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ding
 * @version 1.0
 * @description: TODO 菜品管理
 * @date 2022/6/5 14:48
 */
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    /**
     * 功能描述 :分页查询菜品
     * @param page
     * @param pageSize
     * @param name
     * @return : com.itheima.common.PageResult
     */
    @GetMapping("/page")
    public PageResult page(@RequestParam("page") Integer page,
                           @RequestParam("pageSize") Integer pageSize,
                            @RequestParam(value = "name",required=false)String name){
        Page<Dish> dishPage = dishService.page(page,pageSize,name);
        List<Dish> records = dishPage.getRecords();
        long total = dishPage.getTotal();
        return new  PageResult(Code.OK, MessageConstant.DISH_GET_SUCCESS,records,total);
    }
    /**
     * 功能描述 :新增菜品
     * @param dishDTO
     * @return : com.itheima.common.Result
     */
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){
      int save= dishService.save(dishDTO);
      if (save<1){
          return new Result(Code.ERROR,MessageConstant.DISH_SAVE_ERROR);
      }
      return new Result(Code.OK,MessageConstant.DISH_SAVE_SUCCESS);
    }

    @GetMapping("/list")
    public Result findByCategoryId(@RequestParam(value = "categoryId",required = false) Long categoryId,
                                   @RequestParam(value = "name",required = false) String name){
        // List<Dish> dishList = dishService.findByCategoryId(categoryId,name);
        List<DishDTO> dishList = dishService.findByCategoryId(categoryId,name);
        if (dishList == null){
            dishList = new ArrayList<>();
        }
        return new Result(Code.OK,MessageConstant.DISH_GET_SUCCESS,dishList);
    }
    /**
     * 功能描述 :菜品回显
     * @param id
     * @return : com.itheima.common.Result
     */
    @GetMapping("/{id}")
        public Result findById(@PathVariable ("id") Long id){
            DishDTO findById =  dishService.findById(id);
            if (findById==null){
                return new Result(Code.ERROR,MessageConstant.DISH_GET_ERROR);
            }
            return new Result(Code.OK,MessageConstant.DISH_GET_SUCCESS,findById);
        }
        @PutMapping
        public Result update(@RequestBody DishDTO dishDTO){
            int  update= dishService.update(dishDTO);
            if (update<1){
                return new Result(Code.ERROR,MessageConstant.DISH_UPDATE_ERROR);
            }
            return new Result(Code.OK,MessageConstant.DISH_UPDATE_SUCCESS);
        }
        /**
         * 功能描述 :逻辑删除菜品管理
         * @param ids
         * @return : com.itheima.common.Result
         */
    @DeleteMapping
    public Result deleteByIds(@RequestParam("ids") Long[] ids){
        int row = dishService.deleteByIds(ids);
        if(row<1){
            return new Result(Code.ERROR,MessageConstant.DISH_DELETE_ERROR);
        }
        return new Result(Code.OK,MessageConstant.DISH_DELETE_SUCCESS);
    }
    /**
     * 功能描述 :修改菜品状态
     * @param status
     * @param ids
     * @return : com.itheima.common.Result
     */

    @PostMapping("/status/{status}")
    public Result updateStatusByIds(@PathVariable("status") Integer status,
                                    @RequestParam("ids") Long[] ids){
        dishService.updateStatusByIds(status,ids);
        return new Result(Code.OK,MessageConstant.DISH_UPDATE_SUCCESS);
    }
}
